package demo2;


//如果在父类的构造方法中，调用 子类 和 父类 重写的方法，此时会发生动态绑定
class B{
    public B(){
        func();
    }
    public void func(){
        System.out.println("B.func()");
    }
}

class D extends B{

    private int num=1;

    public void func(){
        System.out.println("D.func()"+num);
    }
}

public class Test {
    public static void main(String[] args) {
        D d=new D();
    }

    public static void main3(String[] args) {
        Rect rect=new Rect();
        Cycle cycle=new Cycle();
        Triangle triangle=new Triangle();
        Shape[] shapes={rect,triangle,cycle,cycle,rect};
        for (Shape shape:shapes) {
            shape.draw();
        }
    }

    public static void main2(String[] args) {
        Rect rect=new Rect();
        Cycle cycle=new Cycle();
        Triangle triangle=new Triangle();

        String[] arr={"cycle","triangle","rect","rect","cycle"};
        for (String x:arr) {
            if(x.equals("cycle")){
                cycle.draw();
            }else if(x.equals("triangle")){
                triangle.draw();
            }else{
                rect.draw();
            }
        }
    }

    public static void drawMap(Shape shape){
        shape.draw();
    }

    public static void main1(String[] args) {
        Rect rect=new Rect();
        Cycle cycle=new Cycle();
        Triangle triangle=new Triangle();

        drawMap(rect);
        drawMap(cycle);
        drawMap(triangle);
    }
}
